package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.SyTimePeriodMapper;
import com.eco.fanliapp.entity.SyTimePeriod;
import com.eco.fanliapp.entity.SyTimePeriodAppointment;
import com.eco.fanliapp.entity.SyTimePeriodGoods;
import com.eco.fanliapp.service.ISyTimePeriodAppointmentService;
import com.eco.fanliapp.service.ISyTimePeriodGoodsService;
import com.eco.fanliapp.service.ISyTimePeriodService;
import com.eco.fanliapp.service.ITaoBaoService;
import com.eco.fanliapp.utils.DateFormatUtils;
import com.eco.fanliapp.vo.respone.ResponeResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-09-09
 */
@Service
public class SyTimePeriodServiceImpl extends ServiceImpl<SyTimePeriodMapper, SyTimePeriod> implements ISyTimePeriodService {

    @Autowired
    private SyTimePeriodMapper syTimePeriodMapper;
    @Autowired
    private ITaoBaoService iTaoBaoService;
    @Autowired
    private ISyTimePeriodAppointmentService iSyTimePeriodAppointmentService;
    @Autowired
    private ISyTimePeriodGoodsService iSyTimePeriodGoodsService;

    @Override
    public List<SyTimePeriod> syTimePeriodList(Integer pageNum, Integer pageSize, int hours, Integer flage) {
        return syTimePeriodMapper.syTimePeriodList(pageNum, pageSize, hours, flage);
    }

    /**
     * 根据商品id刷新限时秒杀商品
     *
     * @param itemId
     * @param userId
     * @return
     */
    @Override
    public ServerResponse timePeriodRefreshDetails(Long itemId, Long userId) {
        ServerResponse response = iTaoBaoService.getHdkSearchItemIdDetail(itemId.toString(), userId, null, "1",null,null);
        Map map = new HashMap();

        if (response.getStatus() == 200) {
            ResponeResults results = (ResponeResults) response.getData();
            map.put("min_id", results.getMin_id());
            map.put("data", results.getData());
            map.put("popType", results.getPopType());
            //查询是否预约
            SyTimePeriodAppointment syTimePeriodAppointment = iSyTimePeriodAppointmentService.findAppointmentByItemIdAndUserId(itemId, userId);
            if (syTimePeriodAppointment == null) {
                map.put("appointment", false);//是否预约
            } else {
                map.put("appointment", true);//是否预约
            }
            //是否开抢
            SyTimePeriodGoods syTimePeriodGoods = iSyTimePeriodGoodsService.findPeriodGoodsById(itemId);
            int hours = new Date().getHours();

            if (syTimePeriodGoods == null) {
                return ServerResponse.createByErrorCodeMessage(201, "暂无数据");
            } else {
                String periodTime = syTimePeriodGoods.getPeriodTime();
                String[] split = periodTime.split(":");
                Integer integer = Integer.valueOf(split[0]);
                if (hours == integer || hours == integer + 1) {
                    map.put("status", 2);//抢购中
                } else if (hours > integer) {
                    map.put("status", 1);//已开抢
                } else {
                    map.put("status", 0);//未开枪
                }
                map.put("itemNum", syTimePeriodGoods.getItemNum());//限定数量
                map.put("periodId", syTimePeriodGoods.getPeriodId());//时间段id
                map.put("periodTime", syTimePeriodGoods.getPeriodTime());//抢购时间段
            }
            map.put("systemDate", DateFormatUtils.dateToString(new Date()));//系统时间
            return ServerResponse.createBySuccess("数据获取成功", map);
        } else if (response.getStatus() == 201) {
            return ServerResponse.createByErrorCodeMessage(201, "暂无数据");
        } else if (response.getStatus() == 601) {
            return ServerResponse.createBySuccessCodeMessage(601, "商品已下架");
        } else {
            return ServerResponse.createByErrorMessage("参数无效-未传入[商品ID]");
        }
    }
}
